﻿Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data.OracleClient

Public Class FabricaADONet

    Private _provider As String

    Private _connectionString As String

    Public Sub New(ByVal _connectionName As String)
        _provider = ConfigurationManager.ConnectionStrings(_connectionName).ProviderName
        _connectionString = ConfigurationManager.ConnectionStrings(_connectionName).ConnectionString
    End Sub


    Public Function GetConnection() As IDbConnection
        Select Case _provider
            Case "System.Data.SqlClient"
                Return New SqlConnection(_connectionString)
            Case "System.Data.OracleClient"
                Return New OracleConnection(_connectionString)
            Case Else
                Throw New ParticaoEquivalenciaException("O provider " & _provider & " não é suportado.")
        End Select
    End Function

    Public Function GetCommand(ByVal _dbConnection As IDbConnection) As IDbCommand
        Dim _dbCommand As IDbCommand
        Select Case _provider
            Case "System.Data.SqlClient"
                _dbCommand = New SqlCommand
                _dbCommand.Connection = _dbConnection
                Return _dbCommand
            Case "System.Data.OracleClient"
                _dbCommand = New OracleCommand
                _dbCommand.Connection = _dbConnection
                Return _dbCommand
            Case Else
                Throw New ParticaoEquivalenciaException("O provider " & _provider & " não é suportado.")
        End Select
    End Function

    Public Function GetCommand(ByVal _SQL As String, ByVal _dbConnection As IDbConnection) As IDbCommand
        Dim _dbCommand As IDbCommand
        Select Case _provider
            Case "System.Data.SqlClient"
                _dbCommand = New SqlCommand
                _dbCommand.Connection = _dbConnection
                _dbCommand.CommandText = _SQL
                Return _dbCommand
            Case "System.Data.OracleClient"
                _dbCommand = New OracleCommand
                _dbCommand.Connection = _dbConnection
                _dbCommand.CommandText = _SQL
                Return _dbCommand
            Case Else
                Throw New ParticaoEquivalenciaException("O provider " & _provider & " não é suportado.")
        End Select
    End Function

End Class
